<!-- 
   图文结构封装组件
  -->
<template>
  <div>
    <el-table :data="tableData" style="width: 100%" size="mini" border>
      <el-table-column type="index" width="50" label="序号" align="center">
      </el-table-column>
      <el-table-column
        label="文件名"
        prop="fileName"
        align="center"
        width="300px;"
      >
        <template slot-scope="scope">
          <div v-if="fileType != 12">
            <el-input
              v-model="scope.row.fileName"
              style="width:280px;"
              size="mini"
              maxlength="100"
            ></el-input>
          </div>
          <div v-if="fileType == 12">
            <el-select
              v-model="scope.row.fileName"
              placeholder="请选择"
              style="width:280px;"
              size="mini"
              filterable
              allow-create
            >
              <el-option
                v-for="(data, index) in otherFileList"
                :key="index"
                :label="data.value"
                :value="data.id"
              >
              </el-option>
            </el-select>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="备注说明"
        prop="fileDesc"
        align="center"
        width="320px;"
      >
        <template slot-scope="scope">
          <div>
            <el-input
              type="textarea"
              v-model="scope.row.fileDesc"
              placeholder="请输入内容"
              maxlength="500"
              show-word-limit
              resize="none"
              :autosize="{ minRows: 3 }"
            ></el-input>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="上传文件" prop="legalDesc" align="center">
        <template slot-scope="scope">
          <div>
            <upload-file
              :imageValue="scope.row.filePath"
              :fileIndex="scope.$index"
              @handlefileSuccess="handlefileSuccess"
            >
            </upload-file>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="更新时间"
        prop="fileDesc"
        align="center"
        width="160px;"
      >
        <template slot-scope="scope">
          <div>
            {{ scope.row.createTime }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="80px;">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            plain
            style="margin: 5px"
            @click="del(scope.$index)"
          >
            删除
          </el-button>
          <a
            v-if="scope.row.filePath"
            :href="scope.row.filePath"
            target="_blank"
            style="color:blue;"
          >
            文件查看
          </a>
        </template>
      </el-table-column>
    </el-table>
    <div class="add-text">
      <span @click="addLine"><i class="el-icon-plus"></i>点击添加</span>
      <span>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      </span>
      <!-- <el-button type="primary" @click="updateSub" size="mini">
        提 交
      </el-button> -->
      <el-button type="primary" size="mini" @click="pushMeg">
        推送消息
      </el-button>
    </div>
  </div>
</template>
<script>
import uploadFile from "@c/uploadFile";
import { APPLYFILELIST, OTHERFILELIST } from "@u/wordbook";
import { submitApplyFileListApi, pushWxTplMsgApi } from "@s/api";
export default {
  name: "filesList",
  props: ["tableData", "fileType", "applyId"],
  data() {
    return {
      url: process.env.VUE_APP_URL + "/sys/oss/upload",
      // url: "https://jsonplaceholder.typicode.com/posts/"
      hideUpload: false,
      imgIndex: 0,
      applyFileList: APPLYFILELIST,
      otherFileList: OTHERFILELIST
    };
  },
  components: {
    uploadFile
  },
  mounted() {},
  methods: {
    del(value) {
      this.$confirm("此操作将删除此文件?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.tableData.splice(value, 1);
        this.$emit("handleTableDate", this.tableData);
      });
    },
    pushMeg() {
      // let name = this.applyFileList.find(v => v.id === this.fileType).value;
      if (this.tableData == null || this.tableData.length == 0) {
        this.$message({
          type: "warning",
          message: "请添加上传的文件！"
        });
        return;
      }
      let name = this.tableData[this.tableData.length - 1].fileName;
      if (name == null || name == "") {
        this.$message({
          type: "warning",
          message: "请填写文件名称！"
        });
        return;
      }
      this.$confirm(
        "点击确定，可推送公众号信息，提醒客户已上传“" + name + "”！",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        let json = {
          applyId: this.applyId,
          filename: name
        };
        pushWxTplMsgApi(json).then(ret => {
          if (ret) {
            this.tableData = ret;
            this.$message({
              type: "success",
              message: "推送成功！"
            });
          }
        });
      });
    },
    updateSub() {
      // 文件校验
      for (var i = 0; i < this.tableData.length; i++) {
        if (this.tableData[i].fileName == "") {
          this.$message({
            type: "warning",
            message: "请输入文件名称后再提交！"
          });
          return;
        }
        if (this.tableData[i].filePath == "") {
          this.$message({
            type: "warning",
            message: "请上传文件后再提交！"
          });
          return;
        }
      }
      let json = {
        applyId: this.applyId,
        fileType: this.fileType,
        applyFilesList: this.tableData
      };
      submitApplyFileListApi(json).then(ret => {
        if (ret) {
          this.tableData = ret;
          this.$message({
            type: "success",
            message: "提交成功！"
          });
        }
      });
    },
    addLine() {
      const name = this.applyFileList.find(v => v.id === this.fileType).value;
      //添加行数
      var newValue = {
        id: "",
        patentApplyId: this.applyId,
        fileType: this.fileType,
        filePath: "",
        fileName: name,
        fileDesc: "",
        createTime: ""
      };
      //添加新的行数
      this.tableData.push(newValue);
      this.$emit("handleTableDate", this.tableData);
    },
    handlefileSuccess(path, index) {
      this.tableData[index].filePath = path;
    }
  }
};
</script>
<style scoped lang="scss">
.sort {
  cursor: pointer;
  color: #409eff;
}
.sort:hover {
  color: #027cfb;
}
.add-text {
  text-align: center;
  span {
    cursor: pointer;
    color: #027cfb;
  }
}
</style>
